<?php
include "TaxCalculatorHelper.php";
include "TitleInsuranceCost.php";
include "TitleInsuranceCalculatorHelper.php";
class TitleInsuranceCalculator  {
    private  $titleInsuranceCosts;
    private  $titleInsuranceCalculatorHelper;
    private  $taxCalculatorHelper;

    public function  TitleInsuranceCalculator($state, $rateInfoProperties) {
        $this->taxCalculatorHelper = new TaxCalculatorHelper();
        $this->titleInsuranceCalculatorHelper = new TitleInsuranceCalculatorHelper();
        $this->titleInsuranceCosts = new TitleInsuranceCosts($state, $rateInfoProperties);
    }

    /**
     * Calculates title insurance costs for purchase, always assumes simultaneous issue as this as better overall
     * pricing.
     *
     * @param purchasePrice    the purchase price in collars/cents.
     * @param loanAmounts
     * @param enhancedCoverage true if enhanced coverage is requested.  @return title insurance cost
     */
    public function purchase($purchasePrice, $loanAmounts, $enhancedCoverage) {
        $loanAmount = $this->taxCalculatorHelper->getLoanTotal($loanAmounts);

        $lendersCost = 0;
        if ($loanAmount > 0) {
            $loanThousands = $this->titleInsuranceCalculatorHelper->roundUp2Thousands($loanAmount);
            $lendersCost = $this->titleInsuranceCosts->getLendersCost($loanThousands);
        }

        $ownersCost = 0;
        if ($purchasePrice > 0.0) {
            $purchaseThousands = $this->titleInsuranceCalculatorHelper->roundUp2Thousands($purchasePrice);

            //If enhanced, its 1.2 x basic plus 50 for simult. coverage of owner and lender.
            //If basic, its the cost of owners plus 50 for simult. coverage.
            //echo $enhancedCoverage; exit(); standard owner cost 164730
            if ($enhancedCoverage) {
                $ownersCost = $this->titleInsuranceCosts->getEnhancedOwnersCost($purchaseThousands);
            } else {
                $ownersCost = $this->titleInsuranceCosts->getStandardOwnersCost($purchaseThousands);
            }
        }
        //echo "$purchasePrice $ownersCost $lendersCost ";197676
        //exit();

        if ($ownersCost > 0.0 && $lendersCost > 0.0) {
            //echo $this->titleInsuranceCosts->getSimultaneousIssueCost();exit();
            $ownersCost += $this->titleInsuranceCosts->getSimultaneousIssueCost();
        }
        
        //echo "$purchasePrice $ownersCost $lendersCost";197726
        //exit();

        $titleServicesCost = $this->titleInsuranceCosts->getTitleServicesCost();

        $settlementFee = $this->titleInsuranceCosts->getSettlementFeeCost();

        $locationSurvey = $this->titleInsuranceCosts->getLocationSurveyCost();
        
        //echo "$titleServicesCost $settlementFee $locationSurvey"; exit();

        return new PurchaseTitleInsuranceCost($purchasePrice, $ownersCost - $lendersCost, $lendersCost, $titleServicesCost, $enhancedCoverage, $settlementFee, $locationSurvey);
    }
    /**
     * Calculates title insurance costs for refinance, lender's policy only
     *
     * @param newLoanAmounts
     * @return title insurance cost
     */
    public function refinance($newLoanAmounts) {

        $newLoanAmount = $this->taxCalculatorHelper->getLoanTotal($newLoanAmounts);

        $thousands = $this->titleInsuranceCalculatorHelper->roundUp2Thousands($newLoanAmount);
        $cost = $this->titleInsuranceCosts->getLendersCost($thousands);
        $titleServicesCost = $this->titleInsuranceCosts->getTitleServicesCost();

        return new RefinanceTitleInsuranceCost($newLoanAmount, $this->titleInsuranceCalculatorHelper->round2($cost, 2), $titleServicesCost);
    }

    /**
     * Calculates the reissue title insurance costs for refinance, lender's policy only.
     *
     * @param newLoanAmounts       the loan amount in dollars/cents.
     * @param existingOwnersPolicy True if borrower has existing owner's policy, else false
     * @param ownersPolicyAmount   amount of existing owner's policy.  Note this is required if existingOwnersPolicy is
     *                             true, else it's ignored.
     *                             <p/>
     *                             From MD PDF:
     *                             Where the owner of property on which application is made for mortgage title insurance has the
     *                             title to such property insured as owner, and all eligibility requirements set forth earlier in this
     *                             manual are met, that borrower shall be entitled to the following reissue rates for a
     *                             standard title insurance policy in the amount up to the face amount of such prior policy rounded
     *                             up to the next thousand:
     * @return cost of new title insurance policy taking into consideration a possible existing policy.
     */
    public function refinanceReissue($newLoanAmounts, $existingOwnersPolicy, $ownersPolicyAmount) 
    {

        $newPolicyCost = $this->refinance($newLoanAmounts);

        $newLoanAmount = $this->taxCalculatorHelper->getLoanTotal($newLoanAmounts);

        $titleServicesCost = $this->titleInsuranceCosts->getTitleServicesCost();

        if (!$existingOwnersPolicy) {
            return new ReissueTitleInsuranceCost($newPolicyCost, 0.0, $titleServicesCost);
        } else {
            // calc discount
            $policyAmount = $ownersPolicyAmount;
            if ($newLoanAmount < $ownersPolicyAmount) {
                $policyAmount = $newLoanAmount;
            }
            $thousands = $this->titleInsuranceCalculatorHelper->roundUp2Thousands($policyAmount);
            $discount = $this->titleInsuranceCosts->getLendersReissueDiscount($thousands);

            return new ReissueTitleInsuranceCost($newPolicyCost, $discount, $titleServicesCost);
        }
    }
}
?>
